﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace Nirvana.SoundCommon
{
    public sealed class PlayerAudioDelay : IPlayerAudio
    {
        private AudioSubItemData audioSubItem;
        private AudioItem audioItem;
        private AudioSourcePool audiopool;
        private AudioSource audioSource;
        private AudioSourceCtrl audioSourceCtl;
        private Transform root;

        public bool IsPlaying { get { return audioSourceCtl.IsPlaying(); } }

        public PlayerAudioDelay(AudioSourcePool pool, AudioItem audioItem, AudioSubItemData audioSubItem, bool loop)
        {
            this.audiopool = pool;
            this.audioItem = audioItem;
            this.audioSubItem = audioSubItem;
            audioSource = pool.Pop(audioSubItem.audioClipName);
            this.audioItem.InitAudioSource(audioSource, audioSubItem);
            audioSource.loop = loop;
            float delay = this.audioItem.Delay + audioSubItem.delay;
            float fadein = audioSubItem.fadeInTime;
            float fadeout = audioSubItem.fadeOutTime;
            this.audioSourceCtl = new AudioSourceCtrl(audioSource, delay, fadein, fadeout);
        }
        public AudioItemMixerGroup GetMixerGroupType()
        {
            return this.audioItem.mixerTypeGroup;
        }
        public AudioSubItemData GetAudioSubItemData()
        {
            return this.audioSubItem;
        }
        /// <summary>
        /// 剩余时间
        /// </summary>
        /// <returns></returns>
        public float GetRamingTime()
        {
            return this.audioSourceCtl.GetClipLength() - this.audioSourceCtl.GetPlayingTime();
        }

        public string GetAudioName()
        {
            return this.audioSource != null && this.audioSource.clip != null ? this.audioSource.clip.name : "";
        }

        public void Set(Vector3 pos)
        {
            this.audioSource.transform.position = pos;
        }

        public void Set(Transform root)
        {
            this.root = root;
        }

        public void Play()
        {
            this.audioSourceCtl.Play();
        }

        public void Update()
        {
            //有父节点，跟随父节点移动
            if (this.root != null)
            {
                this.audioSource.transform.SetPositionAndRotation(this.root.position, this.root.rotation);
                this.audioSource.transform.localScale = this.root.localScale;
            }
            this.audioSourceCtl.UpdateVolume();
        }

        public void Remove()
        {
            if (this.audiopool != null) this.audiopool.Push(this.audioSource);
            if (this.audioItem != null) this.audioItem.Remove(this);
        }

        public IEnumerator WaitFinish()
        {
            return new WaitUntil(() => !this.IsPlaying);
        }

        public void Mute(bool isMute)
        {
            this.audioSource.mute = isMute;
        }
        public void Stop()
        {
            this.audioSourceCtl.Stop();
        }

        public void Dispose()
        {
            audioItem = null;
            audioSubItem = null;
            audiopool = null;
            audioSource = null;
            audioSourceCtl = null;
            root = null;
            audioSubItem = null;

        }
    }
}